﻿@inject IHttpService _httpService

@inject IPopupService PopupService



@inject NavigationManager NavigationManager
@inject IJSRuntime JS


<CnGalWebSite.Shared.MasaComponent.Shared.Cards.TitleTip Title="编辑个人资料"/>

<AutoSaveTip TItem="EditUserDataViewModel" Item="Model" DataLoaded="DataLoaded" @ref="autoSave" />

<div>
    @if (isReady == false)
    {
        <CnGalWebSite.Components.Progressings.ProgressingCard Page Fill />
    }
    else
    {
        <CnGalWebSite.Components.Containers.MasaTitleContainer Title="编辑个人资料" Icon="@IconType.User.ToIconString()" Outline>
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Users.ImageCard Model="Model"/>

         <CnGalWebSite.Components.Containers.MasaTitleContainer Title="相关信息" Icon="@IconType.Link.ToIconString()" Outline Collapable>
                <div class="row">
                    <div class=" col-12 col-md-6  col-lg-4 mb-4">
                        <CnGalWebSite.Shared.MasaComponent.PC.Users.SecurityCards.BindCard/>
                    </div>
                    <div class=" col-12 col-md-6  col-lg-4 mb-4">
                        <CnGalWebSite.Shared.MasaComponent.PC.Users.SecurityCards.QQGroupCard Model="Model" OnChanged="OnChanged" />
                    </div>
                    <div class=" col-12 col-md-6  col-lg-4  mb-4">
                        <CnGalWebSite.Shared.MasaComponent.PC.Users.SecurityCards.UserCertificationCard Model="Model" />
                    </div>
                    <div class=" col-12 col-md-6  col-lg-4  mb-4">
                        <CnGalWebSite.Shared.MasaComponent.PC.Users.SecurityCards.BindSteamCard />
                    </div>
                </div>
            </CnGalWebSite.Components.Containers.MasaTitleContainer>
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Users.MainCard Model="Model" Class="mb-4" />
            <CnGalWebSite.Shared.MasaComponent.Shared.Editors.Buttons.SubmitButton OnSubmit="OnSubmit" Model="Model" />

        </CnGalWebSite.Components.Containers.MasaTitleContainer>



    }

</div>
@code {


    [Parameter]
    public string Id { get; set; }

    public EditUserDataViewModel Model { get; set; } = new EditUserDataViewModel();
    private bool isReady = false;

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    AutoSaveTip<EditUserDataViewModel> autoSave { get; set; }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {

            try
            {
                //获取词条信息
                Model = await _httpService.GetAsync<EditUserDataViewModel>("api/space/edituserdata/");
                isReady = true;
                StateHasChanged();
            }
            catch (Exception ex)
            {
                await ErrorHandler.ProcessError(ex, "获取编辑用户资料失败");
            }
        }
    }

    public void OnChanged()
    {
        StateHasChanged();
        
    }

    private async Task OnSubmit()
    {
        //上传
        try
        {
            var obj = await _httpService.PostAsync<EditUserDataViewModel, Result>("api/space/edituserdata", Model);
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync("保存用户信息失败", obj.Error);
            }
            else
            {
                await autoSave.Clean();

                await PopupService.ConfirmAsync("修改个人资料成功", "我们需要对你的修改进行审核，在通过审核之前你的修改对其他用户不可见");
                NavigationManager.NavigateTo( "/space/index/" + Model.Id);
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "保存用户信息失败");
        }
    }

    private void DataLoaded(EditUserDataViewModel model)
    {
        isReady = false;
        StateHasChanged();
        Model = model;
        isReady = true;
        StateHasChanged();
        
    }

}
